#include "clickedlabel.h"

ClickedLabel::ClickedLabel(QWidget* parent)
    : QLabel(parent), _curstate(ClickLbState::Normal)
{
    setCursor(Qt::PointingHandCursor);
}

// 处理鼠标点击事件
void ClickedLabel::mousePressEvent(QMouseEvent *event){
    if(event->button() == Qt::LeftButton){
        if(_curstate == ClickLbState::Normal){
            qDebug() << "clicked, change to selected hover: " << _selected_hover;
            _curstate = ClickLbState::Selected;
            setProperty("state", _selected_hover);
            repolish(this); // 刷新上面的设置
            update();
        }else{
            qDebug() << "clicked, change to normal hover: " << _normal_hover;
            _curstate = ClickLbState::Normal;
            setProperty("state", _normal_hover);
            repolish(this);
            update();
        }

        emit clicked(this->text(), _curstate); // 高版本的 qt 是不用再 emit clicked(); 的
        return;
    }

    // 调用基类的mouuseePressEvent以保证正常的事件处理
    QLabel::mousePressEvent(event);
}
// 处理鼠标抬起事件
void ClickedLabel::mouseReleaseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton) {
        if(_curstate == ClickLbState::Normal){
            // qDebug()<<"ReleaseEvent , change to normal hover: "<< _normal_hover;
            setProperty("state",_normal_hover);
            repolish(this);
            update();

        }else{
            //  qDebug()<<"ReleaseEvent , change to select hover: "<< _selected_hover;
            setProperty("state",_selected_hover);
            repolish(this);
            update();
        }
        emit clicked(this->text(), _curstate);
        return;
    }
    // 调用基类的mousePressEvent以保证正常的事件处理
    QLabel::mousePressEvent(event);
}

// 鼠标处理悬停进入事件
void ClickedLabel::enterEvent(QEvent *event){
    // 鼠标在这里处理悬停进入的逻辑
    if(_curstate == ClickLbState::Normal){
        qDebug() << "enter, normal change to hover: " << _normal_hover;
        setProperty("state", _normal_hover);
        repolish(this);
        update();
    }else{
        qDebug() << "enter, hover change to normal: " << _normal_hover;
        setProperty("state", _selected_hover);
        repolish(this);
        update();
    }

    QLabel::enterEvent(event);
}
// 处理鼠标悬停离开事件
void ClickedLabel::leaveEvent(QEvent *event){
    // 在这里处理鼠标悬停离开的逻辑
    if(_curstate == ClickLbState::Normal){
        qDebug()<<"leave , hover change to normal: "<< _normal;
        setProperty("state", _normal);
        repolish(this);
        update();
    }else{
        qDebug()<<"leave , normal change to hover: "<< _selected;
        setProperty("state", _selected);
        repolish(this);
        update();
    }
    QLabel::leaveEvent(event);
}

//
void ClickedLabel::SetState(QString normal, QString hover, QString press,
              QString select, QString select_hover, QString select_press){
    _normal = normal;
    _normal_hover = hover;
    _normal_press = press;

    _selected = select;
    _selected_hover = select_hover;
    _selected_press = select_press;

    setProperty("state", normal);
    repolish(this);
}

ClickLbState ClickedLabel::GetCurState(){
    return _curstate;
}

bool ClickedLabel::SetCurState(ClickLbState state){
    _curstate = state;
    if (_curstate == ClickLbState::Normal) {
        setProperty("state", _normal);
        repolish(this);
    }
    else if (_curstate == ClickLbState::Selected) {
        setProperty("state", _selected);
        repolish(this);
    }

    return true;
}


